<template>
  <a-card :bordered="false">

    <!-- 查询区域 -->
    <search ref='search' :config='searchConfig' @getHeight='getHeight' @search='search' />

    <!-- table区域-begin -->
    <div class='page_table'>
      <vxe-grid
        ref='xGrid'
        v-bind='vexConfig'
        :align='left'
        :border='true'
        :checkbox-config="checkboxConfig"
        :columns='columns'
        :data='dataSource'
        :height='pageHeight-seachHeight-90'
        :loading='loading'
        :resizable='true'
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        :showOverflow='true'
        :toolbar-config='tableToolbar'
        size='mini'
        @checkbox-change='selectChangeEvent'
        @checkbox-all='selectChangeEvent'
      >
        <template #toolbar_buttons>
          <div class='table-operator'>
            <a-button type='primary' @click="handleConfig(row)">按线路组新增</a-button>
            <a-button type='primary' @click="handleNetwork(row)">按网点新增</a-button>
<!--            <a-button type='primary' @click="handleConfigTer(row)">按频率新增</a-button>-->
            <a-button type='primary' @click="batchDel">删除</a-button>
            <a-button type="primary" @click="stateChange(1)">启用</a-button>
            <a-button type="primary" @click="stateChange(0)">停用</a-button>
            <a-button type='primary' @click="autogen()">立即生成计划</a-button>
            <!-- <a-button type='primary' @click="batchDel">删除</a-button> -->
            <a-button type="primary" @click="downExportXls('sfaVisitPlan_service')">导出</a-button>
          </div>
        </template>
<!--        详情-->
        <template #detail='{ row,rowIndex }'>
          <a @click='handleDetail(row)'>{{ row.planCode }}</a>
        </template>
        <template #update='{ row }'>
          <a-switch :checked='row.status' size='small' @change='handleStatus(row,$event)' />
        </template>
        <template  #action='{ row,rowIndex }'>
          <a v-has="'SfaVisitGroupList:update'" @click='handleEdit(row)'>编辑</a>
          <a-divider type='vertical' />
          <a href='javascript:' @click='handleBusiness("线路制定",row)'>日志</a>
        </template>
        <template #pager>
          <vxe-pager
            :currentPage.sync='ipagination.current'
            :layouts="['Total', 'PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump','Sizes', 'FullJump']"
            :page-size.sync='ipagination.pageSize'
            :pageSizes='ipagination.pageSizeOptions'
            :total='ipagination.total'
            @page-change='handlePageChange'>
          </vxe-pager>
        </template>
      </vxe-grid>
    </div>
    <!-- table区域-end -->

    <!-- 表单区域 -->
    <!-- <sfaVisitLineEntity-modal ref="modalForm" @ok="modalFormOk"></sfaVisitLineEntity-modal> -->
    <SfaVisitLineList-modal ref="modalForm" @ok="modalFormOk"></SfaVisitLineList-modal>
    <!-- 按线路组新增弹窗 -->
    <sfa-visit-planning-moal
      ref="SfaVisitPlanningMoal"
      modal-width="90%"
      :keyState ='keyState'
      @ok='PlanningMoal'
    >
    </sfa-visit-planning-moal>
<!--    按网点新增弹窗-->
<!--    <sfa-network-molal-->
<!--      ref="SfaNetworkMolal"-->
<!--      modal-width="90%"-->
<!--    ></sfa-network-molal>-->
<!--    &lt;!&ndash;  按频率新增弹窗 &ndash;&gt;-->
<!--    <JVisitGroupModal-->
<!--      ref="JVisitGroupModal"-->
<!--      modal-width="90%"-->
<!--      @ok="handelSave">-->
<!--    </JVisitGroupModal>-->
<!--    按网点新增弹窗-->
<!--    <VisitConfigModal-->
<!--      modal-width="90%"-->
<!--      ref="VisitConfigModal"-->
<!--      @ok="handelSaveOk">-->
<!--    </VisitConfigModal>-->
    <business-log ref='businessLog'></business-log>
  </a-card>
</template>

<script>
import '@/assets/less/TableExpand.less'
import SfaVisitLineListModal from './modules/SfaVisitLineListModal'
import { PageListMixin } from '@/mixins/PageListMixin'
import moment from 'moment'
import JSelectUserPosition from '../../../components/cn-select-popup/JSelectUserPosition'
import { postAction,getAction } from '../../../api/manage'
import SfaVisitPlanningMoal from './modules/SfaVisitPlanningMoal'
import JVisitGroupModal from './modules/JVisitGroupModal'
import SfaNetworkMolal from './modules/SfaNetworkMolal'

import pick from 'lodash.pick'
export default {
  name: 'SfaVisitLineList',
  mixins: [PageListMixin],
  components: {
    SfaVisitLineListModal,
    JSelectUserPosition,
    SfaVisitPlanningMoal,
    JVisitGroupModal,
    SfaNetworkMolal
  },
  data() {
    return {
      keyState: '', //定义新增状态
      description: '拜访明细查询报表',
      operType: 0,
      //查询区域
      searchConfig:[
        {
          type: 'input',
          name: 'visitUserRealName',
          placeholder: '人员姓名'
        },
        {
          type: 'input',
          name: 'visitPosName',
          placeholder: '职位名称'
        },
        {
          type: 'date-range', //范围选择
          range: ['visitDateStart', 'visitDateEnd'],
          // name: 'tempVisitDate',
          format: 'YYYY-MM-DD',
          placeholder: ['开始时间', '结束时间']
        },
        {

          type: 'a-select',
          name: 'makeType',
          option: [{
            name: '按网点制定',
            value: 1
          }, {
            name: '线路组制定',
            value: 2
          },
          ],
          placeholder: '线路规则类型'
        },
      ],
      // 表头
      columns: [
        { type: 'checkbox', width: 50, fixed: 'left' },
        {
          title: '计划编码',
          minWidth:80,
          field: 'planCode',
          slots: {default: 'detail'}
        },
        {
          title: '线路规则类型',
          minWidth:100,
          field: 'makeTypeStr',
        },
        {
          title: '人员姓名',
          minWidth:80,
          field: 'visitUserRealName',
        },
        {
          title: '职位名称',
          minWidth:80,
          field: 'visitPosName'
        },
        {
          title: '所属组织',
          minWidth:80,
          field: 'visitOrgName'
        },
        {
          title: '开始时间',
          minWidth:80,
          field: 'visitDateStart'
        },
        {
          title: '结束时间',
          minWidth:80,
          field: 'visitDateEnd'
        },

        {
          title: '创建人',
          align: 'left',
          minWidth: 80,
          field: 'createName'
        },
        {
          title: '创建时间',
          minWidth: 140,
          field: 'createTime'
        },
        {
          title: '更新人',
          align: 'left',
          minWidth: 80,
          field: 'updateName'
        },
        {
          title: '更新时间',
          align: 'left',
          minWidth: 80,
          field: 'updateTime'
        },
        {
          title: '状态',
          align: 'left',
          field: 'status',
          width: 80,
          fixed: 'right',
          minWidth: 80,
          slots: { default: 'update' }
        },
        {
          title: '操作',
          width: 120,
          fixed: 'right',
          slots: { default: 'action' }
        }
      ],
      isorter: {
        permflag: 'visit_plan_page_impl'
      },
      url: {
        list: window._CONFIG['domianSfaURL'] + '/visit/sfaVisitPlan/findPageList',
        delete: window._CONFIG['domianSfaURL'] + '/visit/sfaVisitLineController/delete',
        detail:window._CONFIG['domianSfaURL'] + '/visit/sfaVisitPlan/getInfo',
        startOrStopUrl:window._CONFIG['domianSfaURL'] + '/visit/sfaVisitPlan/startOrStop',
        status:window._CONFIG['domianSfaURL'] + '/visit/sfaVisitPlan/startOrStop',
        autogen: window._CONFIG['domianSfaURL'] + '/visit/visitLineGenerate/generateVisit',
        deleteBatch: window._CONFIG['domianSfaURL'] + '/visit/sfaVisitPlan/deleteBatch',
        exportXlsUrl: window._CONFIG['domianSfaURL'] + 'visit/sfaVisitLineController/exportXls',
        importExcelUrl: window._CONFIG['domianSfaURL'] + 'visit/sfaVisitLineController/importExcel'
      }
    }
  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },

  methods: {
    PlanningMoal(){
      this.loadData()
    },
    //详情
    handleDetail: async function (record, url, params, method = getAction, modalForm) {
      if (url || this.url.detail) {
        const res = await method(url || this.url.detail, params || { id: record.id })
        if (res && res.result) {
          record = res.result
        }
      }
      this.$refs.SfaVisitPlanningMoal.edit(record)
      this.$refs.SfaVisitPlanningMoal.title = '详情'
      this.$refs.SfaVisitPlanningMoal.disableSubmit = true
    },
    //编辑
    handleEdit: async function (record) {
      if (this.url.detail) {
        const res = await getAction(this.url.detail, { id: record.id })
        if (res.success) {
          if (res.result) {
            record = res.result
          }
        } else {
          this.$message.error(res.message)
        }
      }
      this.$refs.SfaVisitPlanningMoal.edit(record)
      this.$refs.SfaVisitPlanningMoal.title = '编辑'
      this.$refs.SfaVisitPlanningMoal.disableSubmit = false
    },
    //限制当天之前的日期不可选
    disabledDate(current) {
      // return current && current <moment().subtract(1, "days"); //当天之前的不可选，不包括当天
      return current && current < moment().endOf('day') //当天之前的不可选，包括当天
    },
    handle(record, type, modal = 'SfaVisitLineListModal') {
      console.log(this.$refs[modal])
      this.$refs[modal].showModal(record)
      return false
    },
    //频率新增按钮
    handleConfigTer(record, type, modal = 'SfaVisitPlanningMoal') {
      this.$refs[modal].showModal(record)
      this.keyState = "2"
      return false
    },
    //网点新增按钮
    handleNetwork(record, type, modal = 'SfaVisitPlanningMoal'){
      this.$refs[modal].showModal(record)
      this.keyState = "3"
      return false
    },
    //线路组新增按钮
    handleConfig(record, type, modal = 'SfaVisitPlanningMoal') {
      this.$refs[modal].showModal(record)
      this.keyState = "1"
      return false
    },
    searchQuery() {
      if (this.checkParam(false)) {
        this.loadData(1)
      }
    },
    checkParam(ischeck) {
      if (ischeck) {
        if (!this.queryParam.visitPosCode) {
          this.$message.warning('请选择拜访人')
          return false
        }
        if (!this.queryParam.visitDates || this.queryParam.visitDates.length == 0) {
          this.$message.warning('请选择拜访时间')
          return false
        }
        let temp = []
        this.queryParam.visitDates.map(row => {
          temp.push(moment(row).format('YYYY-MM-DD'))
        })
        this.queryParam.visitDates = temp
      } else {
        // this.queryParam.tempVisitDate = moment(this.queryParam.tempVisitDate).format('YYYY-MM-DD')
      }
      return true
    },
    autogen: function() {
      if (!this.url.autogen) {
        this.$message.error('请设置url.autogen属性!')
        return
      }
      var that = this
      getAction(that.url.autogen, {}).then((res) => {
        if (res.success) {
          that.$message.success(res.message)
          this.loadData()
        } else {
          that.$message.warning(res.message)
        }
      })
    },
    async handleClientModal(record) {
      if (this.selectedRowKeys.length != 1) {
        this.$message.warning('请选择一条记录！')
        return
      }
      this.$refs.JSelectClientModal.showModal(this.selectionRows[0].groupCode)
    },
    handleAddUser() {
      this.$refs.VisitConfigModal.showModal()
    },
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>